package com.luyou.awt.event;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 窗口事件适配器
 */
public class WindowAdapterTest {

    private JFrame frame = new JFrame("测试");
    private TextArea ta = new TextArea(6, 40);

    public void init(){
        frame.addWindowListener(new MyListener());
        frame.add(ta);
        frame.pack();
        frame.setVisible(true);
    }

    private class MyListener extends WindowAdapter {

        @Override
        public void windowClosing(WindowEvent e) {
            ta.append("用户关闭窗口\n");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException interruptedException) {
                interruptedException.printStackTrace();
            }
            System.exit(0);
        }
    }

    public static void main(String[] args) {
        new WindowAdapterTest().init();
    }
}
